package com.yonyou.pmclouds.basecom.dao.impl.mybatis.modelParser;

import com.yonyou.pmclouds.basecom.entity.Entity;
import com.yonyou.pmclouds.basecom.util.ReflectUtils;

import java.beans.PropertyDescriptor;
import java.util.HashMap;
import java.util.Map;
 
/**
 * @author zm 
 */ 
public class ModelUtils { 
 
    public static Map<String, Property> getProperties(Entity entity, ColumnTarget columnTarget) { 
        Class<?> modelClass = entity.getClass(); 
        Map<String, Property> properties = getProperties(modelClass, columnTarget); 
        Map<String, Property> results = new HashMap<String, Property>(properties.size()); 
        for (Map.Entry<String, Property> propertyEntry : properties.entrySet()) { 
            Property property = propertyEntry.getValue(); 
            if (columnTarget == ColumnTarget.INSERT || columnTarget == ColumnTarget.UPDATE || columnTarget == ColumnTarget.WHERE) { 
                boolean isIgnore; 
                try { 
                    isIgnore = property.isNullValue(entity); 
                } catch (Exception e) { 
                    isIgnore = true; 
                } 
                if (isIgnore) { // 空值忽略 
                    continue; 
                } 
            } 
 
            results.put(propertyEntry.getKey(), property); 
        } 
 
        return results; 
    } 
 
    /**
     * @param columnTarget 允许为null 
     */ 
    public static Map<String, Property> getProperties(Class<?> modelClass, ColumnTarget columnTarget) { 
        PropertyDescriptor[] propDescriptors = ReflectUtils.getBeanGetters(modelClass); 
        Map<String, Property> properties = new HashMap<String, Property>(propDescriptors.length); 
        for (PropertyDescriptor propertyDescriptor : propDescriptors) { 
            Property property = new Property(modelClass, propertyDescriptor); 
            if (isIllegalPropertyForColumnTarget(property, columnTarget)) { 
                continue; 
            } 
 
            properties.put(property.getName(), property); 
        } 
        return properties; 
    } 
 
    private static boolean isIllegalPropertyForColumnTarget(Property property, ColumnTarget columnTarget) {
        if (property.isTransient()) {
            return columnTarget != ColumnTarget.ORDER || !property.isOrderColumn();
        } 
 
        if (property.isUnableForColumnTarget(columnTarget)) { 
            return true; 
        }
        if (columnTarget == ColumnTarget.UPDATE || columnTarget == ColumnTarget.WHERE) {
            return property.isId();
        } 
 
        return false; 
    } 
 
}